package com.itany.corejava.code08_常用类.object;

import java.util.Objects;

/**
 * @author 石小俊
 * @version 1.0
 * @date 2023年03月14日 13:44
 */
public class Person implements Cloneable{

    private String name;
    private int age;
    private Address address;

    @Override
    protected void finalize() throws Throwable {
        System.out.println("正在销毁");
        super.finalize();
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    //    @Override
//    public boolean equals(Object obj) {
//        // 重写时,可以对方法的参数进行向下转型
//        if (obj instanceof Person) {
//            Person person = (Person) obj;
//            // 比较Person中name与age
//            // 如果两者都相同,则说明对象相等
//            // 由于字符串已经重写了equals,此处可以直接使用
//            return this.name.equals(person.getName()) && this.age == person.getAge();
//        }
//        return false;
//    }


//    @Override
//    public String toString() {
//        return "{name=" + name + ",age=" + age + "}";
//    }


    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
